<template>
  <div style="height: 100%;background-color:white ;">
    <el-row>
      <el-col :span="12">
        <el-button type="primary" @click="openDialog">新建专科库</el-button>
      </el-col>
      <el-col :span="12">
        <div>
          <div style="width: 430px; float: right">
            <el-input v-model="specialtyKnowledgeLib.name"
                      placeholder="请输入名称"
                      @keyup.enter.native="getTableData">
              <i slot="prefix"
                 class="el-input__icon el-icon-search"></i>
              <el-button slot="append"
                         :icon="appendIcon"
                         v-on:click="switchAppend"></el-button>
            </el-input>
          </div>
          <div v-if="showAdvance"
               id="advance"
               style="padding: 10px; float: right;">
            <div>
              <div style="margin-top: 20px; display: flex;">
                <div class="advancedSearchLabel">包含子库</div>
                <div style="width: 217px">
                  <el-select v-model="specialtyKnowledgeLib.knowledgeLibId" clearable filterable
                             size="small">
                    <el-option v-for="item in knowledgeLibList" :key="item.id" :label="item.name"
                               :value="item.id"></el-option>
                  </el-select>
                </div>
              </div>
              <div style="margin-top: 20px; display: flex;">
                <div class="advancedSearchLabel">创建人</div>
                <div style="width: 217px">
                  <el-select
                    v-model="specialtyKnowledgeLib.createdBy"
                    clearable
                    filterable
                    placeholder="请输入创建人"
                    size="small">
                    <el-option
                      v-for="item in $store.state.userList"
                      :key="item.userName"
                      :label="item.name"
                      :value="item.userName">
                    </el-option>
                  </el-select>
                </div>
              </div>
            </div>
            <div style="margin-top: 30px; right: 10px;">
              <el-row>
                <el-col :offset="6" :span="6">
                  <div style="margin-left: 90px;height: 32px; width: 88px;">
                    <el-button size="small" v-on:click="showAdvance = false">取 消
                    </el-button>
                  </div>
                </el-col>
                <el-col :offset="2" :span="2">
                  <div style="margin-left: 30px;height: 32px; width: 88px;">
                    <el-button size="small" v-on:click="reset">重置</el-button>
                  </div>
                </el-col>
                <el-col :offset="2" :span="2">
                  <div style="margin-left: 30px;height: 32px; width: 88px;">
                    <el-button size="small" type="success" v-on:click="getTableData">确 定</el-button>
                  </div>
                </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row style="margin-top: 10px;">
      <el-col :span="24">
        <el-table :data="tableData" :height="tableHeight" border
                  highlight-current-row>
          <el-table-column label="专科库名称"
                           min-width="160"
                           prop="name"></el-table-column>
          <el-table-column label="包含子库"
                           min-width="280"
                           prop="" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ getKnowledgeLibName(scope.row.knowledgeLibIdList) }}
            </template>
          </el-table-column>
          <el-table-column label="创建人"
                           min-width="80"
                           prop="createdBy">
            <template slot-scope="scope">
              {{ UTIL.getNameByUserName(scope.row.createdBy) }}
            </template>
          </el-table-column>
          <el-table-column label="创建时间"
                           min-width="120"
                           prop="createdTime"></el-table-column>
          <el-table-column label="操作"
                           min-width="120">
            <template slot-scope="scope">
              <span><el-link type="primary"
                             @click.prevent="detailById(scope.row.id)">编辑</el-link></span>
              <span v-if="scope.row.knowledgeLibIdList.length > 0"><el-link type="primary"
                                                                            @click.prevent="detailByKnowledgeId(scope.row)">详情</el-link></span>
              <span><el-link type="primary"
                             @click.prevent="deleteById(scope.row)">删除</el-link></span>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-row>
      <el-pagination
        v-if="multiplePages"
        :page-size="specialtyKnowledgeLib.pageSize"
        :page-sizes="[5, 10, 15, 20, 50, 100]"
        :total="totalNum"
        background
        layout="total, sizes, prev, pager, next, jumper"
        style="text-align: right;margin-top: 10px"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"></el-pagination>
    </el-row>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogTitle"
      width="40%">
      <div style="margin-top: 20px; display: flex;">
        <div class="advancedSearchLabel">专科库名称<span style="color: red;">*</span></div>
        <div style="width: 340px">
          <el-input v-model="insertOrUpdateSpecialtyKnowledgeLib.name"
                    maxlength="50"
                    placeholder="请输入名称"
                    show-word-limit
                    type="text"></el-input>
        </div>
      </div>
      <div style="margin-top: 20px; display: flex;">
        <div class="advancedSearchLabel">包含子库</div>
        <div style="width: 340px">
          <el-select
            v-model="insertOrUpdateSpecialtyKnowledgeLib.knowledgeLibIdList"
            collapse-tags
            multiple
            filterable
            placeholder="请选择"
            style="width: 100%;">
            <el-option v-for="item in knowledgeLibList" :key="item.id" :label="item.name"
                       :value="item.id"></el-option>
          </el-select>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="insertOrUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "SpecialtyKnowledgeLibList",
  data() {
    return {
      tableData: [],
      tableHeight: '',
      totalNum: 0,
      multiplePages: true,
      appendIcon: "el-icon-caret-bottom",
      showAdvance: false,
      specialtyKnowledgeLib: {
        currentPage: 0,
        pageSize: 10
      },
      dialogVisible: false,
      dialogTitle: '新建专科库',
      insertOrUpdateSpecialtyKnowledgeLib: {
        name: '',
        knowledgeLibIdList: []
      }
    }
  },
  computed: {
    ...mapState(['knowledgeLibList'])
  },
  methods: {
    getTableData() {
      this.axios.post('/pmph/action/specialtyKnowledgeLib/selectList', this.specialtyKnowledgeLib)
        .then((res => {
          this.tableData = res.data.records;
          this.totalNum = res.data.total;
          this.showAdvance = false;
        }))
    },
    handleCurrentChange(val) {
      this.specialtyKnowledgeLib.currentPage = val;
      this.getTableData();
    },
    handleSizeChange(value) {
      this.specialtyKnowledgeLib.pageSize = value
      this.getTableData();
    },
    switchAppend: function () {
      if (this.showAdvance) {
        this.showAdvance = false;
        this.appendIcon = "el-icon-caret-bottom";
      } else {
        this.showAdvance = true;
        this.appendIcon = "el-icon-caret-top";
      }
    },
    reset() {
      this.specialtyKnowledgeLib.name = '';
      this.specialtyKnowledgeLib.createdBy = '';
      this.specialtyKnowledgeLib.knowledgeLibId = '';
    },
    openDialog() {
      this.insertOrUpdateSpecialtyKnowledgeLib = {};
      this.dialogTitle = '新建专科库';
      this.dialogVisible = true;
    },
    insertOrUpdate() {
      if (UTIL.isEmpty(this.insertOrUpdateSpecialtyKnowledgeLib.name)) {
        this.$message.warning("专科库名称必填");
        return;
      }
      this.axios.get('/pmph/action/specialtyKnowledgeLib/selectAll', {
        params: {
          id: this.insertOrUpdateSpecialtyKnowledgeLib.id
        }
      }).then((res) => {
        if (res.data.filter(item => item.name === this.insertOrUpdateSpecialtyKnowledgeLib.name).length > 0) {
          this.$message.warning("专科库名称重复");
          return;
        }
        this.axios.post('/pmph/action/specialtyKnowledgeLib/insertOrUpdate', this.insertOrUpdateSpecialtyKnowledgeLib)
          .then(() => {
            this.dialogVisible = false;
            this.getTableData();
          })
      });
    },
    getKnowledgeLibName(knowledgeLibIdList) {
      return this.knowledgeLibList.filter(item => knowledgeLibIdList.indexOf(item.id) !== -1)
        .map(item => item.name).join('；');
    },
    detailById(id) {
      this.dialogTitle = '编辑专科库';
      this.axios.get('/pmph/action/specialtyKnowledgeLib/detailById', {
        params: {
          id: id
        }
      }).then((res) => {
        this.insertOrUpdateSpecialtyKnowledgeLib = res.data;
        this.dialogVisible = true;
      })
    },
    detailByKnowledgeId(row) {
      this.$router.push({
        path: '/knowledgeLib',
        query: {
          specialtyId: row.id
        }
      })
    },
    deleteById(row) {
      if (row.knowledgeLibIdList.length > 0) {
        this.$message.warning("该专科库下有子库,请先删除子库");
        return;
      }
      this.$confirm('确认删除该专科库吗?', '确认', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.get('/pmph/action/specialtyKnowledgeLib/deleteById', {
          params: {
            id: row.id
          }
        }).then(() => {
          this.getTableData();
        })
      });
    }
  },
  created() {
    this.tableHeight = CONST.WORKSPACE_HEIGHT - 106;
    this.getTableData();
  }
}
</script>

<style scoped>

</style>
